// Pokeralyzer Form Validation 

$(document).ready(function(){
	var form 			= $("#register");
	var login_name 		= $("#login_name");
	var login_name_desc = $("#login_name_desc");
	var email 			= $("#email_input");
	var email_desc 		= $("#email_desc");
	var pwd1 			= $("#pwd1");
	var pwd1_desc 		= $("#pwd1_desc");
	var pwd2 			= $("#pwd2");
	var pwd2_desc 		= $("#pwd2_desc");
	var check_1			= $("#check_1");
	var check_2			= $("#check_2");
	var check_3			= $("#check_3");
	var check_4			= $("#check_4");
	var send			= $("#send");
	var ready			= $("#ready");

	form.submit(
		function() {
			if(validate_form()) {
				ready.empty();
				//send.removeAttr('disabled', 'disabled');
				return true;
			} 
			else {
				ready.empty();
				ready.prepend('<img src="../gfx/error.jpg"> Every field must be valid before you can continue');
				//send.attr('disabled', 'disabled');
				return false;
			}
		}
	);

	function validate_form() {
		if(
			validate_login_name() &
			validate_first_pwd() &
			validate_email()
		) { return true; } else { return false; } 

	}

	login_name.blur(validate_login_name);
	pwd1.blur(validate_first_pwd);
	pwd2.blur(validate_second_pwd);
	email.blur(validate_email);

	login_name.keyup(validate_login_name);
	pwd1.keyup(validate_first_pwd);
	pwd2.keyup(validate_second_pwd);
	email.keyup(validate_email);
	
	function validate_login_name() {
		if(login_name.val().length > 1) {
			if(login_name.val().length < 3) {
				check_1.empty();
				login_name.addClass("input_error");
				login_name_desc.text("More then two characters, please");
				login_name_desc.addClass("input_error");
			} 
			else if(login_name.val().length > 20) {
				check_1.empty();
				login_name.addClass("input_error");
				login_name_desc.text("20 characters is enough");
				login_name_desc.addClass("input_error");
			}
			else {
				login_name_desc.text("");
				check_1.empty();
				check_1.prepend('<img class="check" src="../gfx/check.jpg">');	
				return true;
			}
		}
	}

	function validate_first_pwd() {
		if(pwd1.val().length > 1) {
			if(pwd1.val().length < 6) {
				check_2.empty();
				pwd1.addClass("input_error");
				pwd1_desc.text(">5 chars, mix of letters numbers");
				pwd1_desc.addClass("input_error");
			} 
			else if(pwd1.val().length > 25) {
				check_2.empty();
				pwd1.addClass("input_error");
				pwd1_desc.text("25 chars is enough");
				pwd1_desc.addClass("input_error");
			}
			else {
				pwd1_desc.text("");
				check_2.empty();
				check_2.prepend('<img class="check" src="../gfx/check.jpg">');	
				validate_second_pwd();
				return true;
			}
		}
	}

	function validate_second_pwd() {
		if(pwd2.val().length > 1) {
			if(pwd2.val().length > 0) {
				if(pwd1.val() != pwd2.val()) {
					check_3.empty();
					pwd2.addClass("input_error");
					pwd2_desc.text("No match yet..");
					pwd2_desc.addClass("input_error");
				}
				else {
					pwd2_desc.text("");
					check_3.empty();
					check_3.prepend('<img class="check" src="../gfx/check.jpg">');	
					return true;
				}
			}
		}
	}

	function validate_email() {
		var regex = /^([\w-]+(?:\.[\w-]+)*)\@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(\d{1,3}\.){3}\d{1,3}\]?)$/i;

		if(email.val().length > 1) {
			if(regex.test(email.val())) {
				check_4.empty();
				email.removeClass("input_error");
				check_4.prepend('<img class="check" src="../gfx/check.jpg">');
				email_desc.text("");
				return true;
			}
			else {
				check_4.empty();
				email.addClass("input_error");
				email_desc.text("Please provide a vaild email");
				email_desc.addClass("input_error");
				return false;
			}
		}
	}
});

